﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.ShprossesBuilder.Modelling.Components;
using System.Drawing;
using ITAP.ShprossesBuilder.Editor;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Properties;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationAlignSelectedShprossesPositions : IOperation
    {
        private ModelControl modelControl;
        public OperationAlignSelectedShprossesPositions()
        {
        }
        #region IOperation Members

        public string Name
        {
            get { return "Выравнивание профилей по позициям"; }
        }

        public void Execute(ModelControl modelControl)
        {
            this.modelControl = modelControl;
            ContextMenuStrip menuStrip = new ContextMenuStrip();

            EventHandler handler = new EventHandler(OnItemClick);
            ToolStripItem horizontal = new ToolStripMenuItem("По горизонтали", Resources.Horizontal.ToBitmap(), handler);
            horizontal.Tag = ComponentAlign.Horizontal;
            menuStrip.Items.Add(horizontal);
            ToolStripItem vertical = new ToolStripMenuItem("По вертикали", Resources.Vertical.ToBitmap(), handler);
            vertical.Tag = ComponentAlign.Vertical;
            menuStrip.Items.Add(vertical);
            menuStrip.Show(Cursor.Position);             
        }

        #endregion

        private void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            ComponentAlign align = (ComponentAlign)item.Tag;

            RectangleF activeModelBoundingBox;
            if (this.modelControl.ModelContainer.ActiveCell != null)
                activeModelBoundingBox = (this.modelControl.ModelContainer.ActiveCell as ModelCell).Model.BoundingBox;
            else
                return;

            foreach (IComponent selectedComponent in this.modelControl.ModelContainer.SelectedComponentsStack)
                if (selectedComponent is Shpross)
                {
                    IComponentAction alignAction = new ActionAlignSelectedShprossesPositions(this.modelControl, align, false, activeModelBoundingBox);
                    alignAction.Action(selectedComponent);
                    break;
                }
            this.modelControl.UpdateModel();
        }
        #region IOperation Members


        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossOperation; }
        }

        #endregion

        #region IOperation Members


        public Image Thumbnail
        {
            get { return Resources.AlignShprossesPositions.ToBitmap(); }
        }

        #endregion

        #region IOperation Members


        public bool CanUndo
        {
            get { return true; }
        }

        #endregion
    }
}
